package com.zjmzxfzhl.modules.purchase.service.impl;

import com.zjmzxfzhl.modules.purchase.controller.dto.PurchasePaymentQueryDto;
import com.zjmzxfzhl.modules.purchase.controller.vo.CalendarContentVo;
import com.zjmzxfzhl.modules.purchase.entity.PurchasePaymentModel;
import com.zjmzxfzhl.modules.purchase.service.PurchaseCalendarService;
import com.zjmzxfzhl.modules.purchase.service.PurchasePaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service
 *
 * @author 庄金明
 */
@Service
public class PurchaseCalendarServiceImpl implements PurchaseCalendarService {

    private static String EMPTY_PAYMENT_SITUATION = "无";

    @Autowired
    private PurchasePaymentService purchasePaymentService;


    @Override
    public Map<String, List<CalendarContentVo>> loadCalendarContent(PurchasePaymentQueryDto queryDto) {
        Map<String, List<CalendarContentVo>> contentVoMap = new HashMap<>(32);
        //查询时间段内的内容
        List<String> pSiList = Arrays.asList(queryDto.getPaymentSituation());
        if (CollectionUtils.isEmpty(pSiList)) {
            return contentVoMap;
        }
        if (pSiList.contains(EMPTY_PAYMENT_SITUATION)) {
            queryDto.setEmptyPaymentSituation(true);
        }
        List<PurchasePaymentModel> payments = purchasePaymentService.listBy(queryDto);
        if (!CollectionUtils.isEmpty(payments)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (PurchasePaymentModel payment : payments) {
                String date = sdf.format(payment.getPaymentDate());
                if (!contentVoMap.containsKey(date)) {
                    List<CalendarContentVo> contentVos = new ArrayList<>();
                    contentVoMap.put(date, contentVos);
                }
                CalendarContentVo contentVo = new CalendarContentVo(payment.getContractCode(), payment);
                contentVoMap.get(date).add(contentVo);
            }
        }
        return contentVoMap;
    }
}
